<?php
/*
公共函数
*/
class RPHP_Functions
{
	
	//返回页码起始与截止
	public function spage($page,$maxpage,$total=9){
		
		if($maxpage>$total){
		
			$num  = ceil($total-1)/2;
			$ps =  $page - $num;
			$pe = $page + $num;
			if($ps<1) $ps=1;
			if($pe>$maxpage) $pe = $maxpage;
			if($page-$num<1) $pe += $num-$ps;
			if($pe+$num>$maxpage) $ps -= $num-1;
		}else{
			$ps = 1;
			$pe = $maxpage;	
		}
		if($ps<1) $ps=1;
		return array('ps'=>$ps,"pe"=>$pe);
	}

	//返回时间格式
	public function returnTime($sec){
	
		if($sec<60) return $sec.'秒前';
		elseif($sec>3600) return floor($sec/3600).'小时前';
		elseif($sec>60) return floor($sec/60).'分钟前';
		
		
	}
	
	//文件大小计算
	public function gsize($size,$unit=1024){
		if($size>$unit){
			$kb = number_format($size/$unit,2,".","");
			if($kb>$unit){
			return number_format($kb/$unit,2,".","").'MB';
			}else{
			return $kb.'KB';
			}
		}else{
		if($size>0) return $size.'B';
		else return '--';
		}
	}
}
?>